using System;

namespace Abide_Halo_Library.Halo_2_Beta_Map
{
    public class IndexEntry : MarshalByRefObject
    {
        public String FileName
        {
            get { return tagName; }
        }
        public String Class
        {
            get { string ret = new string(tag); ret.Replace(" ", string.Empty); return ret; }
        }
        public Int32 Offset
        {
            get { return offset; }
            internal set { offset = value; }
        }
        public Int32 PathOffset
        {
            get { return pathOffset; }
        }
        public Int32 VirtualOffset
        {
            get { return virtualOffset; }
        }
        public Int32 VirtualPathOffset
        {
            get { return pathOffset; }
        }
        public Int32 Ident
        {
            get { return id; }
        }
        public Int32 Size
        {
            get { return size; }
            internal set { size = value; }
        }
        public Int32 Magic
        {
            get { return magic; }
            internal set { magic = value; }
        }

        String tagName;
        Char[] tag;
        Char[] parentTag;
        Char[] grandparentTag;
        Int32 virtualOffset;
        Int32 virtualPathOffset;
        Int32 pathOffset;
        Int32 offset;
        Int32 id;
        Int32 size;
        Int32 magic;

        internal IndexEntry() { }
        public IndexEntry(OBJECT_ENTRY objEntry, Int32 secondaryMagic, String fileName)
        {
            tagName = fileName;
            tag = new char[] { objEntry.tag[3], objEntry.tag[2], objEntry.tag[1], objEntry.tag[0] };
            parentTag = new char[] { objEntry.parentTag[3], objEntry.parentTag[2], objEntry.parentTag[1], objEntry.parentTag[0] };
            grandparentTag = new char[] { objEntry.grandparentTag[3], objEntry.grandparentTag[2], objEntry.grandparentTag[1], objEntry.grandparentTag[0] };
            virtualOffset = objEntry.metaOffset;
            offset = objEntry.metaOffset - secondaryMagic;
            virtualPathOffset = objEntry.pathOffset;
            pathOffset = objEntry.pathOffset - secondaryMagic;
            id = objEntry.id;
            size = objEntry.metaLength;
        }
        public static IndexEntry Create(String tagName, Char[] tag, Char[] parentTag, Char[] grandparentTag, Int32 virtualOffset, Int32 virtualPathOffset, Int32 pathOffset, Int32 offset, Int32 id, Int32 size, Int32 magic)
        {
            return new IndexEntry()
            {
                tagName = tagName,
                parentTag = parentTag,
                grandparentTag = grandparentTag,
                virtualOffset = virtualOffset,
                virtualPathOffset = virtualPathOffset,
                pathOffset = pathOffset,
                offset = offset,
                id = id,
                size = size,
                magic = magic
            };
        }
    }
}
